<template>
  <div class="chooseIconComponent" v-if="item">
    <span class="title">{{ item.title }}</span>
    <div class="right">
      <div class="buttonWrapper">
        <div class="uploadBtn" v-if="!value" @click="openChooseImg">
          <i class="el-icon-plus"></i>
        </div>
        <i v-if="value && String(value).indexOf('icon-') > -1" :class="value" style="width: 100%; display: flex; justify-content: center"></i>
        <span v-if="value != ''" class="repaceImg" @click="updateChooseImg">替换</span>
        <span v-if="value != ''" class="el-icon-error delImg" @click="delImg"></span>
      </div>
      <span class="tips">{{item.tips}}</span>
    </div>
  </div>
</template>
<script>
import { mapState } from "vuex";
import Bus from "@/utils/bus";
export default {
  name: "chooseIconComponent",
  data() {
    return {
      value: null,
      //为了防止页面初始化时，触发不必要的通知更新，此处设置标识符，初始化后2秒置为true
      needUpdate: false,
    };
  },
  created() {},
  props: ["item", "rightIndex", "catalogIndex", "ocIndex", "id","isOnlyIcon"],
  components: {},
  mounted() {
    this.$nextTick(() => {
      this.init();
    });
    setTimeout(() => {
      this.needUpdate = true;
    }, 1000);
    if (this.item.ename == "leftIcon") {
      Bus.$off(`chooseLeftIconAfter${this.id}`);
      Bus.$on(`chooseLeftIconAfter${this.id}`, (params) => {
        this.value = null;
        this.$nextTick(() => {
          this.value = params.url;
          this.updateDataHandle(this.value);
        });
      });
    } 
    if (this.item.ename == "rightIcon") {
      Bus.$off(`chooseRightIconAfter${this.id}`);
      Bus.$on(`chooseRightIconAfter${this.id}`, (params) => {
        this.value = null;
        this.$nextTick(() => {
          this.value = params.url;
          this.updateDataHandle(this.value);
        });
      });
    } 
    if (this.item.ename === "titleIcon") {
      Bus.$off(`chooseTitleIconAfter${this.id}`);
      Bus.$on(`chooseTitleIconAfter${this.id}`, (params) => {
        this.value = null;
        this.$nextTick(() => {
          this.value = params.url;
          this.updateDataHandle(this.value);
        });
      });
    }
    if (this.item.ename === "subTitleIcon") {
      Bus.$off(`chooseSubTitleIconAfter${this.id}`);
      Bus.$on(`chooseSubTitleIconAfter${this.id}`, (params) => {
        this.value = null;
        this.$nextTick(() => {
          this.value = params.url;
          this.updateDataHandle(this.value);
        });
      });
    }
    if (this.item.ename === "moreIcon") {
      Bus.$off(`chooseMoreIconAfter${this.id}`);
      Bus.$on(`chooseMoreIconAfter${this.id}`, (params) => {
        this.value = null;
        this.$nextTick(() => {
          this.value = params.url;
          this.updateDataHandle(this.value);
        });
      });
    }else if(this.item.ename === "backTopIcon"){
      Bus.$off(`choosebackTopIconAfter${this.id}`);
      Bus.$on(`choosebackTopIconAfter${this.id}`, (params) => {
        this.value = null;
        this.$nextTick(() => {
          this.value = params.url;
          this.updateDataHandle(this.value);
        });
      });
    }else if(this.item.ename === "headLeftIcon"){
      Bus.$off(`chooseHeadLeftIconAfter${this.id}`);
      Bus.$on(`chooseHeadLeftIconAfter${this.id}`, (params) => {
        this.value = null;
        this.$nextTick(() => {
          this.value = params.url;
          this.updateDataHandle(this.value);
        });
      });
    }else if(this.item.ename === "editIcon"){
      Bus.$off(`chooseEditIconAfter${this.id}`);
      Bus.$on(`chooseEditIconAfter${this.id}`, (params) => {
        this.value = null;
        this.$nextTick(() => {
          this.value = params.url;
          this.updateDataHandle(this.value);
        });
      });
    }else if(this.item.ename === "seeIcon"){
      Bus.$off(`chooseSeeIconAfter${this.id}`);
      Bus.$on(`chooseSeeIconAfter${this.id}`, (params) => {
        this.value = null;
        this.$nextTick(() => {
          this.value = params.url;
          this.updateDataHandle(this.value);
        });
      });
    }else if(this.item.ename === "delIcon"){
      Bus.$off(`chooseDelIconAfter${this.id}`);
      Bus.$on(`chooseDelIconAfter${this.id}`, (params) => {
        this.value = null;
        this.$nextTick(() => {
          this.value = params.url;
          this.updateDataHandle(this.value);
        });
      });
    }else if(this.item.ename === "btnIcon"){
      Bus.$off(`chooseBtnIconAfter${this.id}`);
      Bus.$on(`chooseBtnIconAfter${this.id}`, (params) => {
        this.value = null;
        this.$nextTick(() => {
          this.value = params.url;
          this.updateDataHandle(this.value);
        });
      });
    }else if(this.item.ename === "dataPageFloatBtnIcon1"){
      Bus.$off(`chooseDataPageFloatBtnIcon1After${this.id}`);
      Bus.$on(`chooseDataPageFloatBtnIcon1After${this.id}`, (params) => {
        this.value = null;
        this.$nextTick(() => {
          this.value = params.url;
          this.updateDataHandle(this.value);
        });
      });
    }else if(this.item.ename === "dataPageFloatBtnIcon2"){
      Bus.$off(`chooseDataPageFloatBtnIcon2After${this.id}`);
      Bus.$on(`chooseDataPageFloatBtnIcon2After${this.id}`, (params) => {
        this.value = null;
        this.$nextTick(() => {
          this.value = params.url;
          this.updateDataHandle(this.value);
        });
      });
    }
  },
  methods: {
    init() {
      if (this.item) {
        this.value = null;
        this.$nextTick(() => {
          let oItem = JSON.parse(JSON.stringify(this.item));
          this.value = oItem.value;
        });
      }
    },
    //更新右侧  更新主区域
    updateDataHandle(value) {
      if (this.needUpdate) {
        let needUpdateMainChild = true;
        // if (this.item.ename === "pageCover") {
        //     needUpdateMain = false;
        // }
        let row = {
          value: value,
          ename: this.item.ename,
          rightIndex: this.rightIndex,
          catalogIndex: this.catalogIndex,
          ocIndex: this.ocIndex,
          needUpdateMainChild: needUpdateMainChild,
          needUpdateMain: false,
        };
        this.$emit("updateParent", JSON.stringify(row));
      }
    },
    //选择图标
    openChooseImg() { 
      Bus.$emit("openChooseImgGroup", {
        ename: this.item.ename,
        controlid: this.id,
        onlyIcon: this.isOnlyIcon||this.item.onlyIcon
      });
    },
    //替换图标
    updateChooseImg() {
      Bus.$emit("openChooseImgGroup", {
        ename: this.item.ename,
        controlid: this.id,
        onlyIcon: this.isOnlyIcon||this.item.onlyIcon
      });
    },
    //删除图标
    delImg() {
      this.value = "";
      this.updateDataHandle("");
    },
  },
  computed: mapState(["decorate"]),
  watch: {},
};
</script>
<style scoped lang="scss">
.chooseIconComponent {
  display: flex;
  width: 100%;
  display: flex;
  align-items: center;
  .title {
    width: 100px;
    min-height: 32px;
    display: flex;
    justify-content: flex-end;
    font-size: 12px;
    align-items: center;
    padding-right: 10px;
  }
  .right {
    display: flex;
    align-items: center;
    flex: 1;
    width: calc(100% - 100px);
    .buttonWrapper {
      position: relative;
      display: flex;
      align-items: center;
      justify-content: center;
      height: 40px;
      background-color: #f5f5f5;
      width: 40px;
      .uploadBtn {
        width: 100%;
        height: 100%;
        display: flex;
        box-sizing: border-box;
        border-radius: 2px;
        justify-content: center;
        align-items: center;
        background-color: #fff;
        flex-direction: column;
        position: relative;
        cursor: pointer;
        border: 1px solid #e9edef;
        i {
          color: #0079fe;
          font-weight: bold;
          font-size: 14px;
          margin-top: 5px;
          margin-bottom: 5px;
        }
      }
      img {
        width: 100%;
        height: 100%;
        display: flex;
      }
      .repaceImg,
      .delImg {
        position: absolute;
        z-index: 3;
        opacity: 0.5;
        display: none;
        cursor: pointer;
        &:hover {
          cursor: pointer;
        }
      }
      .repaceImg {
        left: 0;
        bottom: 0px;
        width: 100%;
        height: 20px;
        font-size: 12px;
        background-color: rgba(0, 0, 0, 0.8);
        color: #fff;
        justify-content: center;
      }
      .delImg {
        right: -4px;
        top: -4px;
      }
      &:hover {
        .repaceImg,
        .delImg {
          display: flex;
        }
      }
    }
    .tips{font-size:12px;color:#999;margin-left:10px;}
  }
}
</style>